/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedoit.gui;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import org.freedoit.FreeDoitConfig;
import org.freedoit.om.Task;

/**
 *
 * @author wanglei
 */
public class TaskItemRenderer implements ListCellRenderer, MouseListener {
    
    private DoitFrame frame;
    
    public TaskItemRenderer(DoitFrame frame) {
        super();
        this.frame = frame;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (value instanceof TaskItemPanel) {
            TaskItemPanel panel = (TaskItemPanel)value;
            if (isSelected) {
                panel.setBackground(list.getSelectionBackground());
            } else {
                panel.setBackground(list.getBackground());
            }
            return panel;
        }
        else
        {
            return null;
        }
    }

    public void mouseClicked(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private boolean isCheckBoxLocation(TaskItemPanel panel, Point p) {
        Point pCheckBox = panel.getJCheckBoxTaskFinished().getLocation();
        int startX = pCheckBox.x;
        int endX = pCheckBox.x + panel.getJCheckBoxTaskFinished().getWidth();
        
        return (p.x > startX) && (p.x < endX);
    }

    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Object source = e.getSource();
        if (source instanceof JList) {
            JList list = (JList)source;
            Point p = e.getPoint();
            int index = list.locationToIndex(p);
            
            // Not within the valid range
            if (index < 0 || index >= list.getModel().getSize()) {
                return;
            }
            
            Object data = list.getModel().getElementAt(index);
            if (data instanceof TaskItemPanel) {
                TaskItemPanel panel = (TaskItemPanel)data;
                if (isCheckBoxLocation(panel, p)) {
                    Task task = panel.getTask();
                    task.setFinished(!task.getFinished());
                    
                    // remote sync
                    if (task.getTaskId() == null) {
                        frame.remoteSyncTask(task, FreeDoitConfig.ADD_TASK_SERVICE);
                    } else {
                        frame.remoteSyncTask(task, FreeDoitConfig.FINISH_TASK_SERVICE);
                    }
                    
                    DefaultListModel dlm = (DefaultListModel)list.getModel();
                    dlm.remove(index);
                    list.setModel(dlm);
                    list.repaint();
                }
            }
        }
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
